package com.aefottt.struggle_star.activity;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.aefottt.struggle_star.R;
import com.aefottt.struggle_star.base.BaseActivity;
import com.aefottt.struggle_star.fragment.ConcentrateFragment;
import com.aefottt.struggle_star.fragment.MainFragment;

public class MainWrapperActivity extends BaseActivity {
    private MainFragment mainFragment;
    private ConcentrateFragment concentrateFragment;
    private ViewPager vp;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main_wrapper;
    }

    @Override
    protected void initView() {
        super.initView();
        vp = findViewById(R.id.vp_main_wrapper);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        if (mainFragment == null) {
            mainFragment = new MainFragment();
            mainFragment.setVpChangeListener(vp::setCurrentItem);
        }
        if (concentrateFragment == null) {
            concentrateFragment = new ConcentrateFragment();
        }
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(),
                FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                if (position == 1) {
                    return mainFragment;
                } else if (position == 0) {
                    return concentrateFragment;
                }
                return null;
            }

            @Override
            public int getCount() {
                return 2;
            }
        });
        vp.setCurrentItem(1);
    }

    public interface VpChangeListener {
        void changeVp(int item);
    }
}